#include <iostream>
using namespace std;

class Solution
{
public:
    bool isValidSerialization(string preorder)
    {
        int n = preorder.length();
        int in = -1, out = 0;  // 根节点出度2入度0；中间节点出度2入度1；叶子节点出度0入度1；
        for (int i = 0; i < n; i++)
        {
            if (preorder[i] == ',')
            {
                cout << "逗号" << endl;
                continue;
            }
            in++;
            if (out - in < 0)
            {
                cout << "out-in<0" << endl;
                return false;
            }
            if (preorder[i] != '#')
            {
                out += 2;
                while (i < n - 1 && preorder[i + 1] != ',' && preorder[i + 1] != '#')
                    i++;
            }
            cout << in << " " << out << endl;
        }
        return out - in == 0;
    }
};

int main()
{
    Solution s;
    cout << s.isValidSerialization(" 9, #, 92, #, #");
}
